package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.*;
import com.caifu.pojo.*;
import com.caifu.service.UserDeviceService;
import com.caifu.util.tools.DateUtils;
import com.caifu.vo.DeviceVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tww
 * ClassName: UserDeviceServiceImpl.java、
 * createTime: 2020年12月14日 14:25:30
 **/
@Service
public class UserDeviceServiceImpl implements UserDeviceService {

    @Resource
    private DeviceMapper mapper;

    @Resource
    private UserDDeptMapper userDDeptMapper;

    @Resource
    private OrgDeptMapper orgDeptMapper;

    @Resource
    private UserOrgMapper userOrgMapper;

    @Resource
    private UserCDUserMapper userCDUserMapper;

    @Resource
    private CDRecordMapper recordMapper;

    @Override
    public PageInfo<DeviceVo> getDeviceList(SysUser user, String name, String status, Integer currentPage, Integer pageSize) {
        HashMap<String, Object> params = new HashMap<>(10);
        params.put("name", name);
        params.put("status", status);
        params.put("orgId", user.getRelativeId());
        PageHelper.startPage(currentPage, pageSize);
        List<DeviceVo> deviceVos = mapper.getDeviceList(params);
        for (DeviceVo deviceVo : deviceVos) {
            if ("1".equals(deviceVo.getStatus())) {
                deviceVo.setStatus("在线");
            } else {
                deviceVo.setStatus("离线");
            }
        }
        PageInfo<DeviceVo> pageInfo = new PageInfo<>(deviceVos);
        return pageInfo;
    }

    @Override
    public CDevice getDeviceById(String id) {
        return mapper.selectById(id);
    }

    @Override
    public List<String> getDeviceNameByDeptId(Integer id) {
        return mapper.getDeviceNameByDeptId(id);
    }

    @Override
    public Map<String, Object> getOptionalDept(Integer relativeId) {
        Map<String, Object> map = new HashMap<>(10);
        List<COrgDept> cOrgDepts = orgDeptMapper.selectList(new QueryWrapper<COrgDept>()
                .eq("status", 1)
                .eq("org_id", relativeId));
        map.put("deptName", cOrgDepts);
        return map;
    }

    @Override
    public List<COrgDept> getSelectedDept(String deviceId, Integer relativeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("deviceId", deviceId);
        map.put("orgId", relativeId);
        List<COrgDept> deptNames = mapper.getSelectedDept(map);
        return deptNames;
    }

    @Override
    public List<CDevice> getDeviceListAll(SysUser user) {
        List<CDevice> devices = new ArrayList<>();
        //判断当前用户类型
        if ("1".equals(user.getUserType())) {
            devices = mapper.getDeviceListAll(user.getRelativeId());
        } else {
            devices = mapper.getDeviceListByDeptId(user.getRelativeId());
        }
        return devices;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Map listDeviceUser() throws Exception {
        Map map = new HashMap();
        try {
            List<Map<String, Object>> list = userCDUserMapper.listDeviceUser();
            List<CDRecord> rList = new ArrayList<>();
            List<CDUser> uList = new ArrayList<>();
            for (Map<String, Object> objectMap : list) {
                CDRecord record = new CDRecord();
                record.setStatus("0");
                record.setCreateTime(new Date());
                record.setDeviceId(Integer.valueOf(objectMap.get("deviceId").toString()));
                record.setIssuedState("0");
                record.setUserNo(Integer.valueOf(objectMap.get("userNo").toString()));
                rList.add(record);

                CDUser user = new CDUser();
                user = userCDUserMapper.selectById(Integer.valueOf(objectMap.get("id").toString()));
                System.out.println(objectMap.get("userNo"));
                user.setIsIssued("1");
                uList.add(user);

            }
            rList.forEach(model -> recordMapper.insert(model));
            uList.forEach(model -> userCDUserMapper.updateById(model));
            map.put("code", 0);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 9);
        }
        return map;
    }

    @Override
    public Map checkGuestCode(String sn, String code, String date) {
        Map map = new HashMap();
        try {
            String time = "";
            CDevice device = mapper.getDeviceInfo(sn);
            if(device == null){
                map.put("code", 302);
                map.put("msg", "未查询到相关设备，请检查设备编号是否正确！");
                return map;
            }
            CDUser user = mapper.getCdUser(device.getId()+"", code, DateUtils.simpleDate(date.substring(0,8)), date.substring(8,14));
            if(user == null){
                map.put("code", 303);
                map.put("msg", "校验失败！");
                return map;
            } else {
                map.put("code", 0);
                map.put("msg", "校验通过！");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 9);
        }
        return map;
    }

    @Override
    public List<CDevice> listDevive() throws Exception {
        try{
            return mapper.listDevive();
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }


}
